<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'user_id',
        'title',
        'content',
        'rating',
        'favorite',
        'like'
    ];

    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
    }

    public function comments()
    {
        return $this->hasMany(Comment::class,'post_id','id');
    }

    public function getPostComments()
    {
        $posts = Post::with('comments')->get();
        return $posts;
    }
    // 获取文章、评论和评论作者
    public function getPostUser()
    {
        $postUser = Post::with('comments.user')->get();
        return $postUser;
    }
    

    public function getPostCommentsRate()
    {
        $postCommentRate = Post::with(['comments' => function ($query) {
            $query->where('rating', '>', 2); // 只过滤评论，不影响文章
        }])->get();
        return $postCommentRate;
    }

    
    


}